package com.warehouse.controller;

import com.github.pagehelper.PageInfo;
import com.warehouse.dto.PageResult;
import com.warehouse.dto.Result;
import com.warehouse.dto.ResultCode;
import com.warehouse.entity.ProType;
import com.warehouse.service.ProTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: warehouse
 * @description: 产品类别
 * @author: xiaojiu
 * @create: 2021-01-13 20:56
 */
@RestController
@RequestMapping("/api")
@Api(tags = "产品类别")
public class ProTypeController {
    @Autowired(required = false)
    private ProTypeService proTypeService;

    /**
     * 分页查询所有产品类别
     *
     * @param currentPage
     * @param pageSize
     * @param sortName
     * @return
     */
    @ApiOperation(value = "页查询所有产品类别", httpMethod = "GET")
    @GetMapping("/findAllProType")
    public Result<PageResult<ProType>> findProType(@RequestParam Integer currentPage, Integer pageSize, String sortName) {
        PageResult<ProType> proTypes = proTypeService.findAllProType(currentPage, pageSize, sortName);
        return new Result<>(ResultCode.SUCCESS, proTypes);
    }

    /**
     * 添加产品类别
     */
    @ApiOperation(value = "添加所有产品类别", httpMethod = "POST")
    @PostMapping("/addProType")
    public Result<ProType> addProType(@RequestBody ProType proType) {
        System.out.println(proType);
        ProType proType1 = proTypeService.insertProType(proType);
        return new Result<>(ResultCode.SUCCESS, proType1);
    }

    /**
     * 添加产品类别
     */
    @ApiOperation(value = "根据id删除产品类别", httpMethod = "DELETE")
    @RequestMapping(value = "/delProType", method = RequestMethod.DELETE)
    public Result delProType(String id) {
        System.out.println(id);
        boolean b = proTypeService.delProTypeByid(id);
        if (b) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 修改产品类别
     */
    @ApiOperation(value = "修改产品类别", httpMethod = "POST")
    @PostMapping("/editProType")
    public Result editProType(@RequestBody ProType proType) {
        System.out.println(proType);
        boolean b = proTypeService.updateProType(proType);
        if (b) {
            return new Result(ResultCode.SUCCESS);
        }
        return new Result(ResultCode.FAIL);
    }

    /**
     * 模糊查询
     */
    @ApiOperation(value = "多条件模糊查询", httpMethod = "PUT")
    @PostMapping("/findProType")
    public Result findProType(@RequestBody ProType proType) {
        List<ProType> proTypes = proTypeService.findProType(proType);
        System.out.println(proTypes);
        if (proTypes != null) {
            return new Result(ResultCode.SUCCESS, proTypes);
        }
        return new Result(ResultCode.FAIL);
    }
}
